mui.init(); //页面初始化
var mask = mui.createMask(); //遮罩层
/*	
 * xiao  
 * 2018/9/29 
 * 1.设置密码注册
 */

//注册
window.onload = function() {
	mui.plusReady(function() {
		var self = plus.webview.currentWebview();
		var rold = self.sta; //注册类型
		var phone = self.phone; //手机号
		var mobile = self.mobile; //手机号
//		console.log(mobile)
		var verification_code = self.verification_code; //验证码
		var referral_code = self.Invitation_code; //邀请码
		var region = self.region; //地区
		var region_init = region.split(" "); //截取地区
		var receiver_state = region_init[0]; //省
		var receiver_city = region_init[1]; //市
		var receiver_district = region_init[2]; //区
		//注册接口
		$(document).on("tap", "#forget_pwd_btn", function() {
			var pwdRerg = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,15}$/; //正则验证 
			var password = $("#new_pwd").val(); //获取密码
			if(!password) {
				mui.alert("请输入有效密码");
				return false;
			}
			if(!pwdRerg.test(password)) {
				mui.alert("密码格式不正确");
				$("#new_pwd").val("");
				return false;
			}
			plus.nativeUI.showWaiting("加载中...");
			mask.show(); //显示遮罩层
			mui.ajax(user_url + '/api/register', {
				data: {
					'mobile': mobile,
					'phone': phone,
					'password': password,
					'referral_code': referral_code,
					'rold': rold,
					'verification_code': verification_code,
					'sign': 2,
					"receiver_city": receiver_city,
					"receiver_district": receiver_district,
					"receiver_state": receiver_state
				},
				dataType: 'json', //服务器返回json格式数据
				type: 'post', //HTTP请求类型
				headers: {
					'Content-Type': 'application/json'
				},
				timeout: 10000, //超时时间设置为10秒；
				success: function(data) {
					if(data.code == 10000) {
						if(rold == 2) {
							var my_phone = data.resp.phone; //个人手机号					
							var user_id = data.resp.id; //个人user_id
							localStorage.setItem("my_phone", my_phone);
							localStorage.setItem("user_id", user_id);
							mui.toast("注册成功");
							var is_realname = data.resp.is_realname; //是否实名
							if(is_realname == 0) {
								mui.openWindow({
									url: '../my/name_authentication.html',
									createNew: true
								})
							} else {
								window.top.location.href = "../my/my_user.html"
							}
							plus.nativeUI.closeWaiting(); //关闭等待
							mask.close(); //关闭遮罩层
							return;
						}
					}

				},
				error: function(xhr, type, errorThrown) {
					//异常处理；
					console.log(type);
					plus.nativeUI.closeWaiting(); //关闭等待
					mask.close(); //关闭遮罩层
					var error_msg = JSON.parse(xhr.responseText);
					if(error_msg.msg == "手机号已注册") {
						return;
					} else {
						mui.toast(error_msg.msg)
					}
				}
			});
		})
	});
}